package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.RetailLevelActivity;
import cn.xfdzx.android.apps.shop.bean.HomeRetailBeen;
import cn.xfdzx.android.apps.shop.contract.BannerListener;
import cn.xfdzx.android.apps.shop.view.MyGridView;

/**
 * @author gzk
 * @date 2020/3/27 0027
 * description：banner上下内容区域，不包含每日特价
 */
public class HomeBannerViewHolder extends RecyclerView.ViewHolder  {


    @BindView(R.id.ll_banner)
    Banner banner;

    @BindView(R.id.gv_home_class)
    MyGridView gvHomeClass;//中间显示5个分类
    @BindView(R.id.iv_home_activity)
    ImageView ivHomeActivity;//活动图
   public BannerListener bannerListener;

    private Context mContext;
    private HomeClassGVAdapter homeClassGVAdapter;

    public HomeBannerViewHolder(Context mContext, View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.mContext = mContext;

        init();

    }

    private void init() {
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());

        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
//
        //设置轮播间隔时间
        banner.setDelayTime(3000);
//
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
//        //设置指示器的位置，小点点，左中右。
//        banner.setIndicatorGravity(BannerConfig.CENTER)
//                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
//                .setOnBannerListener(this);

        homeClassGVAdapter = new HomeClassGVAdapter(mContext);
        gvHomeClass.setAdapter(homeClassGVAdapter);
        gvHomeClass.setSelector(new ColorDrawable(Color.TRANSPARENT));
        bannerListener=new BannerListener() {
            @Override
            public void onItemClick(boolean isStart) {
                if (isStart) {
                    banner.startAutoPlay();

                } else {

                    banner.stopAutoPlay();
                }
            }
        };

    }


    public void setData(List<HomeRetailBeen.DataBean.BannersBean> beanList, List<HomeRetailBeen.DataBean.IndexWholesaleCategoryBean> indexList) {

        List<String> imageUrls = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            imageUrls.add(beanList.get(i).getPic());
        }
        //设置图片网址或地址的集合
        banner.setImages(imageUrls);

        //必须最后调用的方法，启动轮播图。
        banner.start();


        homeClassGVAdapter.setData(indexList);
        gvHomeClass.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, RetailLevelActivity.class);
                intent.putExtra("title_name", indexList.get(i).getName());
                intent.putExtra("type_id", indexList.get(i).getId()+"");
                mContext.startActivity(intent);
            }
        });

        //广告图
        ivHomeActivity.setBackgroundResource(R.drawable.image_shichang_center);


    }


    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            RoundedCorners roundedCorners = new RoundedCorners(40);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//         RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);

            imageView.setPadding(50, 0, 50, 0);

            Glide.with(context).load(path).apply(options.transform(new CenterCrop(), roundedCorners)).into(imageView);
        }

    }




}



